package com.md.api.common.utils;

import com.md.api.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.mock.web.MockMultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

@Slf4j
public class FileUtils {

	private FileUtils() {
	}

	public static void uploadFile(byte[] file, String filePath, String fileName) {
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		FileOutputStream out = null;
		String fileAllPath = new StringBuilder(filePath).append(File.separator).append(fileName).toString();
		log.debug("上传文件路径:{}", fileAllPath);
		try {
			out = new FileOutputStream(fileAllPath);
			out.write(file);
			out.flush();
		} catch (IOException e) {
			log.error("uploadFile", e);
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					log.error("uploadFile - close", e);
				}
			}
		}
	}

	public static byte[] toByteArray(InputStream input) throws IOException {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		copy(input, output);
		return output.toByteArray();
	}

	public static int copy(InputStream input, OutputStream output) throws IOException {
		long count = copyLarge(input, output);
		if (count > 2147483647L) {
			return -1;
		}
		return (int) count;
	}

	public static long copyLarge(InputStream input, OutputStream output) throws IOException {
		byte[] buffer = new byte[4096];
		long count = 0L;
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
			count += n;
		}
		return count;
	}

	/**
	 * 将网络文件转换为文件流
	 * @param imageUrl
	 * @return
	 */
	public static MultipartFile fileUrlConvertToMultipartFile(String fileName,String imageUrl) {
		try {
			// 将在线图片地址转换为URL对象
			URL url = new URL(imageUrl);
			// 打开URL连接
			URLConnection connection = url.openConnection();
			// 转换为HttpURLConnection对象
			HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
			// 获取输入流
			InputStream inputStream = httpURLConnection.getInputStream();

			String contentType = connection.getContentType();
			// 读取输入流中的数据，并保存到字节数组中
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int bytesRead;
			while ((bytesRead = inputStream.read(buffer)) != -1) {
				byteArrayOutputStream.write(buffer, 0, bytesRead);
			}
			// 将字节数组转换为字节数组
			byte[] bytes = byteArrayOutputStream.toByteArray();
			// 创建ByteArrayInputStream对象，将字节数组传递给它
			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
			// 创建MultipartFile对象，将ByteArrayInputStream对象作为构造函数的参数
			MultipartFile multipartFile = new MockMultipartFile(fileName, fileName, contentType, byteArrayInputStream);
			return multipartFile;
		}catch (IOException ex){
			ex.printStackTrace();
			return null;
		}
	}
	private static String getFileName(URL url) {
		String[] pathSegments = url.getPath().split("/");
		return pathSegments[pathSegments.length - 1];
	}
}
